package com.slavshik.components {
	import flash.display.StageAlign;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.utils.getDefinitionByName;

	/**
	 * @author Alexander Slavshik <as.kvint@gmail.com>
	 * created 25.10.2010
	 */
	public class Preloader extends MovieClip {
		protected var _mainClassName : String = 'Main';
		protected var _loaded:Number = 0;
		
		public function Preloader(className:String = 'Main') {
			_mainClassName = className;
			loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
			loaderInfo.addEventListener(Event.COMPLETE, onComplete);
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.showDefaultContextMenu = false;
			stage.addEventListener(Event.RESIZE, onResize);
			onResize(null);
		}

		protected function onComplete(event : Event) : void {
			loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
			loaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			
			nextFrame();
			var mainClass : Class = getDefinitionByName(_mainClassName) as Class;
			addChild(new mainClass as DisplayObject);
		}

		protected function onProgress(event : ProgressEvent) : void {
			_loaded = event.bytesLoaded / event.bytesTotal;
		}
		protected function onResize(event : Event) : void {
			
		}
	}
}
